package com.hyt.it.ogt.controller.ykcj.grade;

import cn.hutool.core.util.StrUtil;
import com.hyt.it.ogt.controller.base.BaseController;
import com.hyt.it.ogt.ykcj.common.annotation.Log;
import com.hyt.it.ogt.ykcj.common.constant.Constants;
import com.hyt.it.ogt.ykcj.common.core.domain.AjaxResult;
import com.hyt.it.ogt.ykcj.common.core.page.PageDomain;
import com.hyt.it.ogt.ykcj.common.core.page.TableDataInfo;
import com.hyt.it.ogt.ykcj.common.core.page.TableSupport;
import com.hyt.it.ogt.ykcj.common.enums.BusinessType;
import com.hyt.it.ogt.ykcj.common.enums.FileType;
import com.hyt.it.ogt.ykcj.common.utils.DateUtils;
import com.hyt.it.ogt.ykcj.common.utils.poi.ExcelUtil;
import com.hyt.it.ogt.ykcj.domain.exam.ExamPostInfo;
import com.hyt.it.ogt.ykcj.domain.grade.ServantExamineeInfo;
import com.hyt.it.ogt.ykcj.domain.vo.*;
import com.hyt.it.ogt.ykcj.service.grade.IGradeAnalysisDataService;
import com.hyt.it.ogt.ykcj.service.grade.IServantExamineeGradeImportService;
import com.hyt.it.ogt.ykcj.service.grade.IServantExamineeInfoService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 公务员考生信息Controller
 * 
 * @author admin
 * @date 2021-09-09
 */
@Api(tags = "grade.3.公务员考生信息", value = "公务员考生信息请求接口")
@ApiSort(value = 203)
@RestController
@RequestMapping("/grade/examineeInfo")
public class ServantExamineeInfoController extends BaseController
{
    @Autowired
    private IServantExamineeInfoService servantExamineeInfoService;

    @Autowired
    private IServantExamineeGradeImportService servantExamineeGradeImportService;

    @Autowired
    private IGradeAnalysisDataService gradeAnalysisDataService;

    /**
     * 查询公务员考生信息列表
     */
    @ApiOperation("查询公务员考生信息列表")
    @GetMapping("/list")
    public TableDataInfo list(ServantExamineeInfo servantExamineeInfo)
    {
        startPage();
        List<ServantExamineeInfo> list = servantExamineeInfoService.selectServantExamineeInfoList(servantExamineeInfo);
        return getDataTable(list);
    }

    /**
     * 导出公务员考生信息列表
     */
    @ApiOperation("导出公务员考生信息列表")
    @Log(title = "公务员考生信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ServantExamineeInfo servantExamineeInfo)
    {
        List<ServantExamineeInfo> list = servantExamineeInfoService.selectServantExamineeInfoList(servantExamineeInfo);
        ExcelUtil<ServantExamineeInfo> util = new ExcelUtil<ServantExamineeInfo>(ServantExamineeInfo.class);
        return util.exportExcel(list, "公务员考生信息数据");
    }

    /**
     * 获取公务员考生信息详细信息
     */
    @ApiOperation("获取公务员考生信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(servantExamineeInfoService.selectServantExamineeInfoById(id));
    }

    /**
     * 新增公务员考生信息
     */
    @ApiOperation("新增公务员考生信息")
    @Log(title = "公务员考生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ServantExamineeInfo servantExamineeInfo)
    {
        return toAjax(servantExamineeInfoService.insertServantExamineeInfo(servantExamineeInfo));
    }

    /**
     * 修改公务员考生信息
     */
    @ApiOperation("修改公务员考生信息")
    @Log(title = "公务员考生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ServantExamineeInfo servantExamineeInfo)
    {
        return toAjax(servantExamineeInfoService.updateServantExamineeInfo(servantExamineeInfo));
    }

    /**
     * 删除公务员考生信息
     */
    @ApiOperation("删除公务员考生信息")
    @Log(title = "公务员考生信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(servantExamineeInfoService.deleteServantExamineeInfoByIds(ids));
    }

    /**
     * 删除公务员考生信息及成绩数据
     */
    @ApiOperation("删除公务员考生信息及成绩数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "考生ID", dataType = "String", required = true),
            @ApiImplicitParam(name = "subjectId", value = "科目ID", dataType = "String")
    })
    @Log(title = "公务员考生信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteExamineeGradeById/{id}")
    public AjaxResult deleteExamineeGradeById(@PathVariable("id") String id, String subjectId) {
        return toAjax(servantExamineeInfoService.deleteExamineeGradeById(id, subjectId));
    }

    /**
     * 获取公务员考生成绩信息列表
     */
    @ApiOperation("获取公务员考生成绩信息列表")
    @GetMapping("/getList")
    public TableDataInfo getList(ServantExamineeInfoVo servantExamineeInfoVo) {
        String queryParam = servantExamineeInfoVo.getAdmissionNo();
        Pattern r = Pattern.compile("\\d+");
        Matcher m = r.matcher(queryParam);
        if (!m.matches()) { // 非纯数字则为姓名
            servantExamineeInfoVo.setName(queryParam);
            servantExamineeInfoVo.setAdmissionNo(null);
        }
        startPage();
        List<ServantExamineeInfoVo> list = servantExamineeInfoService.selectExamineeGradePage(servantExamineeInfoVo);
        return getDataTable(list);
    }

    /**
     * 获取公务员考生成绩信息详细
     */
    @ApiOperation("获取公务员考生成绩信息详细(公务员)")
    @ApiImplicitParam(name = "id", value = "考生ID", dataType = "String", required = true)
    @GetMapping(value = "/getExamineeGrade/{id}")
    public AjaxResult getExamineeGrade(@PathVariable("id") String id) {
        return AjaxResult.success(servantExamineeInfoService.selectExamineeGradeById(id));
    }

    /**
     * 保存公务员考生成绩信息
     */
    @ApiOperation("保存公务员考生成绩信息")
    @Log(title = "公务员考生信息", businessType = BusinessType.INSERT)
    @PostMapping("/saveExamineeGrade")
    public AjaxResult saveExamineeGrade(@RequestBody ServantExamineeInfoVo servantExamineeInfoVo) throws Exception {
        return AjaxResult.success(servantExamineeInfoService.saveExamineeGrade(servantExamineeInfoVo));
    }

    @ApiOperation("成绩归档")
    @Log(title = "成绩归档", businessType = BusinessType.UPDATE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "examId", value = "考试ID", dataType = "String", required = true),
            @ApiImplicitParam(name = "ids", value = "考生ID（多个可“,”隔开）", dataType = "String")
    })
    @PostMapping("/archive")
    public AjaxResult archive(@RequestParam String examId, String ids) {
        return toAjax(servantExamineeInfoService.updateArchiveByIds(Constants.ARCHIVED_STATUS_Y, DateUtils.getNowDate(), examId , ids));
    }

    @ApiOperation("导出成绩(公务员)")
    @GetMapping(value = "/downloadExamineeGradeList")
    public AjaxResult downloadExamineeGradeList(ServantExamineeInfoVo servantExamineeInfoVo) {
        return AjaxResult.success(servantExamineeInfoService.downloadExamineeGradeList(servantExamineeInfoVo, "N"));
    }

    @ApiOperation("下载成绩导入模板(公务员)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "examId", value = "考试ID", dataType = "String", required = true),
            @ApiImplicitParam(name = "subjectId", value = "科目ID", dataType = "String")
    })
    @GetMapping(value = "/downloadTemplate/{examId}")
    public AjaxResult downloadTemplate(@PathVariable("examId") String examId, String subjectId) {
        ServantExamineeInfoVo servantExamineeInfoVo = new ServantExamineeInfoVo();
        servantExamineeInfoVo.setExamId(examId);
        servantExamineeInfoVo.setSubjectId(subjectId);
        return AjaxResult.success(servantExamineeInfoService.downloadExamineeGradeList(servantExamineeInfoVo, "Y"));
    }

    @ApiOperation("导入成绩(公务员)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "examId", value = "考试ID", dataType = "String", required = true),
            @ApiImplicitParam(name = "subjectId", value = "科目ID", dataType = "String")
    })
    @Log(title = "导入成绩", businessType = BusinessType.IMPORT)
    @PostMapping("/importGradeInfo")
    public AjaxResult importGradeInfo(MultipartFile multipartFile, @RequestParam("examId") String examId, String subjectId) throws Exception {
        // 校验文件格式
        if(null == multipartFile){
            return AjaxResult.error("文件为空！");
        }
        // 校验文件格式
        String fileName = multipartFile.getOriginalFilename();
        if(StrUtil.isBlank(fileName)){
            return AjaxResult.error("文件名不能为空");
        }
        String fileType = fileName.substring(fileName.indexOf("."));
        Object[] objects;
        if (FileType.FILE_TYPE_DBF.getType().equals(fileType)) {
            objects =  servantExamineeGradeImportService.importtDbfFile(multipartFile, examId, subjectId);
        } else if (FileType.FILE_TYPE_XLSX.getType().equals(fileType) || FileType.FILE_TYPE_XLS.getType().equals(fileType)) {
            objects = servantExamineeGradeImportService.importtExcelFile(multipartFile, examId, subjectId);
        } else {
            return AjaxResult.error("文件类型错误！");
        }
        return new Boolean(objects[0].toString()) ? AjaxResult.success("导入成功", objects[1]):AjaxResult.error("导入失败", objects[1]);
    }

    /**
     * 获取面试名单
     */
    @ApiOperation("获取面试名单")
    @GetMapping("/getInterviewList")
    public AjaxResult getInterviewList(ExamScoreLineReq examScoreLineReq){
        InterViewRsp interViewRsp = servantExamineeInfoService.getInterViewList(examScoreLineReq);
        return AjaxResult.success(interViewRsp);
    }

    /**
     * 查询机构性质列表
     */
    @ApiOperation("查询机构性质列表")
    @GetMapping("/queryJgxzList")
    public AjaxResult queryJgxzList(String examId){
        List<JgxzVo> jgxzVos = servantExamineeInfoService.getJgxzList(examId);
        return AjaxResult.success(jgxzVos);
    }


    /**
     * 查询机构招考单位信息
     */
    @ApiOperation("查询机构招考单位信息")
    @GetMapping("/queryExamScoreLine")
    public AjaxResult queryExamScoreLine(ExamScoreLineReq examScoreLineReq) {
        ExamScoreLineRsp rsp = servantExamineeInfoService.queryExamScoreLine(examScoreLineReq);
        return AjaxResult.success(rsp);
    }

    /**
     * 查询单位招考职位信息列表
     */
    @ApiOperation("查询单位招考职位信息列表")
    @GetMapping("/queryUnitPostInfo")
    public TableDataInfo queryUnitPostInfo(ExamScoreLineReq examScoreLineReq) {
        startPage();
        List<UnitPostVo> rsp = servantExamineeInfoService.queryUnitPostInfo(examScoreLineReq);
        return getDataTable(rsp);
    }

    /**
     * 查询单位招考职位分数线信息
     */
    @ApiOperation("查询单位招考职位分数线信息")
    @GetMapping("/queryUnitScoreLine")
    public AjaxResult queryUnitScoreLine(ExamScoreLineReq examScoreLineReq) {
        PostScoreInfoVo rsp = servantExamineeInfoService.queryUnitScoreLine(examScoreLineReq);
        return AjaxResult.success(rsp);
    }

    /**
     * 获取面试考生排名
     */
    @ApiOperation("获取面试考生排名")
    @GetMapping("/getInterviewRankList")
    public TableDataInfo getInterviewRankList(ExamScoreLineReq examScoreLineReq){
        startPage();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        Integer rankingBaseNum = pageSize * (pageNum - 1);
        List<InterviewRank> interviewRankList = servantExamineeInfoService.getInterviewRankList(examScoreLineReq, rankingBaseNum);
        return getDataTable(interviewRankList);
    }

    /**
     * 获取面试考生排名上方职位信息
     */
    @ApiOperation("获取面试考生排名上方职位信息")
    @GetMapping("/getPostInfo")
    public AjaxResult getPostInfo(ExamScoreLineReq examScoreLineReq){
        ExamPostInfo examPostInfo = servantExamineeInfoService.getPostInfo(examScoreLineReq);
        return AjaxResult.success(examPostInfo);
    }

    /**
     * 获得历史考试名称列表
     */
    @ApiOperation("获得历史考试名称列表")
    @GetMapping("/getHistoryExamNameList")
    public AjaxResult getHistoryExamNameList(HistoryExamReq historyExamReq){
        List<HistoryScoreLineVo> historyScoreLineVos = servantExamineeInfoService.getHistoryExamNameList(historyExamReq);
        return AjaxResult.success(historyScoreLineVos);
    }

    @ApiOperation("获得职位历史分数线")
    @GetMapping("/getHistoryPostScoreLine")
    public AjaxResult getHistoryPostScoreLine(ExamScoreLineReq examScoreLineReq){
        List<HistoryScoreLineVo> list = servantExamineeInfoService.getHistoryPostScoreLine(examScoreLineReq);
        return AjaxResult.success(list);
    }

    @ApiOperation("手动触发任务接口")
    @GetMapping("/manualAnalysisDataTask")
    public AjaxResult manualAnalysisDataTask() {
        servantExamineeInfoService.updateInterViewAndQualifiedList();
        gradeAnalysisDataService.gradeDataStatistics();
        return AjaxResult.success(0);
    }

    @ApiOperation("测试接口（更新合格状态和面试状态代码）")
    @PostMapping("/updateInterViewAndQualifiedList")
    public AjaxResult updateInterViewAndQualifiedList() {
        servantExamineeInfoService.updateInterViewAndQualifiedList();
        return AjaxResult.success(0);
    }
}
